import java.util.ArrayList;
import java.util.List;

/**
 * School 类（学院）：
 * 约束：由 1 到多个系构成；拥有 0 到多名学生。
 */
public class School {
    private String name;
    // 组合关系：1 到多个系 (多重性 1..*)
    private List<Department> departments; 
    // 关联关系：0 到多名学生 (多重性 0..*)
    private List<Student> students; 

    public School(String name) {
        this.name = name;
        this.departments = new ArrayList<>();
        this.students = new ArrayList<>();
    }
    
    public void addDepartment(Department department) {
        this.departments.add(department);
    }
    
    public void addStudent(Student student) {
        this.students.add(student);
    }
    
    // 验证方法：确保满足最小约束 (至少一个系)
    public boolean isValid() {
        return departments.size() >= 1; 
    }
    
    // --- 主测试方法 ---
    public static void main(String[] args) {
        School university = new School("University of Excellence");

        // 1. 创建系 (Department): 满足 School (1..*) Department 组合约束
        Department csDept = new Department("Computer Science");
        university.addDepartment(csDept);

        // 2. 创建课程 (Subject)
        Subject uml = new Subject("UML Modeling");
        Subject java = new Subject("Advanced Java");
        csDept.addSubject(uml);
        csDept.addSubject(java); // Department 提供至少 1 门 Subject 约束满足

        // 3. 创建指导教师 (Instructor)
        Instructor drSmith = new Instructor("Dr. Smith", csDept);
        csDept.addInstructor(drSmith); // Department 拥有至少 1 位 Instructor 约束满足
        
        // 4. 教授课程：满足 Instructor 教授 1-3 门 Subject 约束
        drSmith.addSubject(uml);
        drSmith.addSubject(java);
        
        // 5. 创建学生 (Student)
        Student alice = new Student("Alice");
        university.addStudent(alice); // School 拥有 0..* Student 约束满足
        
        // 6. 选课：满足 Student 参加 1-5 门 Subject 约束
        alice.enrollSubject(uml); 
        alice.enrollSubject(java);

        System.out.println("--- 学院系统结构验证 ---");
        System.out.println("学院: " + university.name + " (" + (university.isValid() ? "有效" : "无效") + ")");
        System.out.println("  > 系数量: " + university.departments.size());
        
        // 验证 Department 约束
        System.out.println("\n--- 系结构验证 ---");
        System.out.println("系: " + csDept.getName() + " (" + (csDept.isValid() ? "有效" : "无效") + ")");
        System.out.println("  > 教师数量: " + csDept.getInstructors().size());
        System.out.println("  > 课程数量: " + csDept.getSubjects().size());

        // 验证 Instructor 约束
        System.out.println("\n--- 教师结构验证 ---");
        System.out.println("教师: " + drSmith.getName() + " (" + (drSmith.isValid() ? "有效" : "无效") + ")");
        System.out.println("  > 教授课程数: " + drSmith.getSubjects().size());
        
        // 验证 Student 约束
        System.out.println("\n--- 学生结构验证 ---");
        System.out.println("学生: " + alice.getName() + " (" + (alice.isValid() ? "有效" : "无效") + ")");
        System.out.println("  > 选课数: " + alice.getSubjects().size());
    }
}